package cd.cdyb.sms.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;



/**
 * 阿里云短信
 * @author Leon
 *  2020年4月16日 下午3:48:39
 */
public class AliSms {
  private final static Logger log = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

  public static void sendSms(String phone, String content, String accessId, String accessKey, String signName, String authTemplateCode) {
    log.info("短信 phone="+phone+",code="+content);

    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessId, accessKey);
    IAcsClient client = new DefaultAcsClient(profile);

    CommonRequest request = new CommonRequest();
    request.setSysMethod(MethodType.POST);
    request.setSysDomain("dysmsapi.aliyuncs.com");
    request.setSysVersion("2017-05-25");
    request.setSysAction("SendSms");
    request.putQueryParameter("RegionId", "cn-hangzhou");
    request.putQueryParameter("PhoneNumbers", phone);
    request.putQueryParameter("SignName", signName);
    request.putQueryParameter("TemplateCode", authTemplateCode);
    request.putQueryParameter("TemplateParam", "{\"code\": \"" + content + "\"}");

    try {
      CommonResponse response = client.getCommonResponse(request);
    } catch (ClientException e) {
      e.printStackTrace();
    }
  }

 /* *//**
   * 短信发送
   * @param accessKeyId
   * @param accessKeySecret
   * @param productname
   * @param templateCode
   * @param phone
   * @param code
   *//*
  public static void sendSms(String accessKeyId,String accessKeySecret,String productname,String templateCode,String phone,String code) {
    log.info("短信验证码phone="+phone+",code="+code);

    //可自助调整超时时间
    System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
    System.setProperty("sun.net.client.defaultReadTimeout", "10000");

    //初始化acsClient,暂不支持region化
    IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
    try {
      DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
    } catch (ClientException e) {
      log.error("连接阿里云短信服务异常", e);
    }
    IAcsClient acsClient = new DefaultAcsClient(profile);

    SendSmsRequest request = new SendSmsRequest();
    request.setPhoneNumbers(phone);
    request.setSignName(productname);
    request.setTemplateCode(templateCode);
    request.setTemplateParam("{\"code\":\""+code+"\"}");

    SendSmsResponse sendSmsResponse=null;
      try {
      sendSmsResponse = acsClient.getAcsResponse(request);
    } catch (Exception e) {
      log.error("连接阿里云短信服务异常", e);
    }

    *//**发送短信失败错误打印*//*
    if(sendSmsResponse==null){
      log.error("短信发送失败,phone="+phone+"code="+code);
      return;
    }
    if(!"OK".equals(sendSmsResponse.getMessage())){
      log.error("阿里云短信错误信息="+sendSmsResponse.getMessage());
    }
  }

  public static void main(String[] args){
    sendSms("LTAI4GAsJaYiUsFwv9kfS8EC","fYXwAg5YcT0qAEy67kdkh7IkRVjVjC","迅驰出行","SMS_228340170","13551018971","8527");
  }
*/
}
